<?php

/**
 * Class for rendering an ordered or unordered list element.
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
class ViewOl extends ViewHtmlElement {

    /**
     * The list items.
     *
     * @var array
     */
    private $li = array();

    /**
     * Array assigning classes to columns (e.g. "hidden").
     *
     * @var array
     */
    private $liClass = array();

    /**
     * The constructor.
     *
     * @param array $fields
     */
    public function __construct(array $fields = array()) {

        $fields["tag"] = isset($fields["is_ordered"]) ? ($fields["is_ordered"] ? "ol" : "ul") : "ul";
        $fields["data"] = isset($fields["data"]) ? $fields["data"] : array();

        parent::__construct($fields);

        if (count($this->getData())) {
            foreach ($this->getData() as $key => $datum) {
                $radioValue = is_array($datum) ? $datum[0] : $datum;
                $this->li[$key] = ViewHtmlElement::elementInit("li");
                $this->li[$key]->setId($key);
                if (is_array($datum)) {
                    $this->li[$key]->innerHtml = $datum;
                }
                else {
                    $this->li[$key]->addText($datum);
                }
                $this->addInnerHtml($this->li[$key]);
            }
        }
    }

    /**
     * Setting a list item "$li" to a class "$className".
     *
     * @param int $li
     * @param string $className
     */
    public function addClassToListItem($li, $className) {
        $this->liClass[$li] = $className;
    }

    /**
     * Updating list items which might have been modified by addClassToListItem.
     */
    private function updateListItems() {
        if (count($this->li) > 0) {
            foreach ($this->li as $key => $listItem) {
                if (count($this->liClass) > 0) {
                    foreach ($this->liClass as $li => $className) {
                        if ($key == $li) {
                            $this->li[$key]->addClass($className);
                        }
                    }
                }
            }
        }
    }

    /**
     * Viewing
     */
    public function view() {
        $this->updateListItems();
        if (count($this->li) == 0) {
            return NULL;
        }
        return parent::view();
    }

}